package com.shfl.task.config;

import com.shfl.task.component.InitComponent;
import com.shfl.task.listener.MyJobListener;
import com.shfl.task.logic.service.TaskLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * Quartz 调度配置
 */
@Configuration
public class QuartzConfig {

    Logger logger = LoggerFactory.getLogger(QuartzConfig.class);

    @Autowired
    private InitComponent initComponent;

    @Autowired
    private TaskLogService taskLogService;

    /**
     * 定义quartz 调度工厂
     * <p>
     * 会自动执行 调度工厂
     */
    @Bean("schedulerFactory")
    @Order(999)
    public SchedulerFactoryBean schedulerFactoryBean() {
        SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
        //用于quartz集群，QuartzScheduler启动时更新已存在的job
        factoryBean.setOverwriteExistingJobs(true);
        //延时启动，应用启动1秒后
        factoryBean.setStartupDelay(1);
        //注册触发器
        factoryBean.setTriggers(initComponent.getCronTriggers());
        //factoryBean.setGlobalTriggerListeners(new MyTriggerListener(taskLogService));
        factoryBean.setGlobalJobListeners(new MyJobListener(factoryBean, taskLogService));
        logger.info("scheduler 初始化成功！");
        return factoryBean;
    }


}
